package com.neusoft.elmboot.po.wallet;

import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Getter;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Getter
@TableName(value = "wallettransaction")
public class WalletTransaction {
    /**
     * 交易序列号
     */
    private String transId;

    /**
     * 付款人id
     */
    private String fromWalletId;

    /**
     * 收款人id
     */
    private String toWalletId;

    /**
     * 交易额度
     */
    private BigDecimal amount;

    /**
     * 是否处于冻结状态
     */
    private Boolean isFrozen = true;

    /**
     * 交易是否有效
     */
    private Boolean isValid = true;

    private LocalDateTime createTime;

    public WalletTransaction(String fromWalletId, String toWalletId, BigDecimal amount){
        String timeStr = String.valueOf(System.currentTimeMillis());
        String random = String.valueOf((int)(Math.random() * 9 + 1) * 10000);
        this.transId = timeStr + random;
        this.fromWalletId = fromWalletId;
        this.toWalletId = toWalletId;
        this.amount = amount;
        this.createTime = LocalDateTime.now();
    }

    @Operation(summary = "交易有效")
    public void setValid(){
        this.isValid = true;
    }

    @Operation(summary = "交易无效")
    public void setInvalid(){
        this.isValid = false;
    }

    @Operation(summary = "冻结")
    public void freeze() {
        this.isFrozen = true;
    }

    @Operation(summary = "解冻")
    public void unfreeze() {
        this.isFrozen = false;
    }
}
